package com.yanjr.modules.ad.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.yanjr.common.persistence.Page;
import com.yanjr.common.utils.Global;
import com.yanjr.common.utils.StringUtils;
import com.yanjr.common.web.BaseController;
import com.yanjr.modules.ad.entity.AdSpace;
import com.yanjr.modules.ad.service.AdSpaceService;


/**
 * 广告位Controller
 *完成对广告位的增加，删除，修改，查看<br>
 *    对应jsp页面在 /WEB-INF/views/modules/advertise 目录下<br>
 * 其中所要注意的功能如下：<br>
 *
 * @ClassName: AdSpaceController 
 * @author yanjr
 * @date 2015年2月03日 上午9:17:33
 * @since 0.2 @version 0.3
 */
@Controller
@RequestMapping(value = "${adminPath}/ad/space")
public class AdSpaceController extends BaseController {

	@Autowired
	private AdSpaceService adSpaceService;
	
	
	/**
	 * 
	 * 根据id获得广告位数据
	 * @param id
	 * @return AdSpace
	 *
	 * @datetime 2015年1月23日上午9:12:04
	 * @author lipingping
	 * @version 0.3
	 */
	@ModelAttribute
	public AdSpace get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return adSpaceService.get(id);
		}else{
			return new AdSpace();
		}
	}
	
    /**
	 * 
	 * 获取广告位数据列表
	 * @param model 返回模型
	 * @return modules/" + "advertise/adSpaceList
	 *
	 * @datetime 2015年1月23日上午9:12:04
	 * @author lipingping
	 * @version 0.3
	 */
	@RequestMapping(value={"list", ""})
	public String list(AdSpace adSpace, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<AdSpace> page = adSpaceService.find(new Page<AdSpace>(request, response), adSpace); 
        model.addAttribute("page", page);
		return "modules/advertise/adSpaceList";
	}
	
	@RequestMapping(value = {"treeData"})
	public String data( Model model) {
//		List<Map<String, Object>> mapList = Lists.newArrayList();
        List<AdSpace> spacels = adSpaceService.allData(); 
        model.addAttribute("spacels",spacels);
        return "modules/" + "advertise/adspaceTree";
	}

    /**
	 * 
	 * 查看广告位数据内容
	 * @param model 返回模型
	 * @return "modules/" + "advertise/adSpaceForm
	 *
	 * @datetime 2015年1月23日上午9:12:04
	 * @author lipingping
	 * @version 0.3
	 */
	@RequestMapping("form")
	public String form(AdSpace adSpace, Model model) {
		model.addAttribute("adSpace", adSpace);
		return "modules/advertise/adSpaceForm";
	}

/**
	 * 
	 * 保存广告位数据内容
	 * @param model 返回模型
	 * @return redirect:"+Global.getAdminPath()+"/myadvertise/space/?repage
	 *
	 * @datetime 2015年1月23日上午9:12:04
	 * @author lipingping
	 * @version 0.3
	 */
	@RequestMapping(value = "save")
	public String save(AdSpace adSpace, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, adSpace)){
			return form(adSpace, model);
		}
		adSpaceService.save(adSpace);
		addMessage(redirectAttributes, "保存广告位数据'" + adSpace.getName() + "'成功");
		return "redirect:"+Global.getAdminPath()+"/ad/space/?repage";
	}
	
/**
	 * 
	 * 删除广告位数据内容
	 * @param model 返回模型
	 * @return redirect:"+Global.getAdminPath()+"/myadvertise/space/?repage
	 *
	 * @datetime 2015年1月23日上午9:12:04
	 * @author lipingping
	 * @version 0.3
	 */
	@RequestMapping(value = "delete")
	public String delete(String id, RedirectAttributes redirectAttributes) {
		adSpaceService.delete(id);
		addMessage(redirectAttributes, "删除广告位数据成功");
		return "redirect:"+Global.getAdminPath()+"/myadvertise/space/?repage";
	}
	
//	@RequestMapping(value = "enable")
//	public String enableSpage(String id, RedirectAttributes redirectAttributes) {
//		adSpaceService.enableById(id);
//		addMessage(redirectAttributes, "启用广告位数据成功");
//		return "redirect:"+Global.getAdminPath()+"/myadvertise/space/?repage";
//	}

}
